package cn.com.ccxe.dao.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.ccxe.core.entity.Keyword;
import cn.com.ccxe.core.entity.MonitorType;
import cn.com.ccxe.dao.DBHandler;
import cn.com.ccxe.dao.KeywordDao;
import cn.com.ccxe.dao.OperationStatus;
import cn.com.ccxe.dao.parser.KeywordParser;
import cn.com.ccxe.dao.parser.MonitorTypeParser;

public class KeywordDaoImpl extends DBHandler implements KeywordDao {
	
	@SuppressWarnings("unused")
	private static final Logger logger = LoggerFactory.getLogger(KeywordDaoImpl.class);
			
	/** {@inheritDoc} */
	public OperationStatus add(Keyword keyword) {
		String sql = "INSERT INTO C_KEYWORD(ID,TEXT) "
				+ "VALUES(SEQ_C_KEYWORD.NEXTVAL,?)";
		
		return insertOrUpdateOrDelete(sql, new Object[] { keyword.getText() });
	}
	
	/** {@inheritDoc} */
	public OperationStatus del(int keywordId) {
		String sql = "DELETE C_KEYWORD WHERE ID=?";
		return insertOrUpdateOrDelete(sql, new Object[] { keywordId });
	}
	
	/** {@inheritDoc} 
	 */
	public OperationStatus update(int id, Keyword keyword) {
		String sql = "UPDATE C_KEYWORD " + "SET TEXT=? "
					+ "WHERE ID=?";
		
		Object[] objs = new Object[] { keyword.getText(),id };
		return insertOrUpdateOrDelete(sql, objs);
	}

	/** {@inheritDoc} */
	public OperationStatus update(Keyword keyword) {
		return update(keyword.getId(), keyword);
	}

	/** {@inheritDoc} */
	public OperationStatus updateMonitorType(int keywordId, int oldMonitorTypeId,
			int newMonitorTypeId) {
		String sql = "UPDATE C_KEYWORD_TYPE " +
		"SET MONITOR_TYPE_ID=? " +
		"WHERE KEYWORD_ID=? AND MONITOR_TYPE_ID=?";

		Object[] objs = new Object[] {
				newMonitorTypeId, 
				keywordId, 
				oldMonitorTypeId};
		
		return insertOrUpdateOrDelete(sql,objs);
	}

	/** {@inheritDoc} */
	public OperationStatus delRelationshipForMonitorType(int keywordId,int monitorTypeId) {
		String sql = 
				"DELETE C_KEYWORD_TYPE " +
				"WHERE KEYWORD_ID=? " +
				"AND MONITOR_TYPE_ID=?";
		Object [] parameters = new Object[]{
				keywordId,
				monitorTypeId
		};
		return insertOrUpdateOrDelete(sql, parameters);
	}

	/** {@inheritDoc} */
	public Keyword getKeyword(int keywordId) {
		String sql = 
			" SELECT A.ID keywordId,A.TEXT keywordText " +
			" FROM C_KEYWORD A " +
			" WHERE A.ID=?";
		
		return queryObject(sql, new Object[]{keywordId}, new KeywordParser());
	}
	
	/** {@inheritDoc} */
	public List<Keyword> getAllKeywordInType(int monitorTypeId) {
		String sql = "SELECT A.ID keywordId,A.TEXT keywordText "
					+ "FROM C_KEYWORD A, C_KEYWORD_TYPE B " 
					+ "WHERE B.MONITOR_TYPE_ID=? AND A.ID=B.KEYWORD_ID ";
		return queryList(sql, new Object[]{monitorTypeId}, new KeywordParser());
	}

	public List<Keyword> getAllKeyword() {
		String sql = 
				"SELECT A.ID keywordId,A.TEXT keywordText " +
				"FROM C_KEYWORD A";
		return queryList(sql, null, new KeywordParser());
	}

	/** {@inheritDoc} 
	 *	 现数据库中只有C_KEYWORD_TYPE与之相关联。
	 */
	public int count(int keywordId) {
		String sql = "SELECT COUNT(0) FROM C_KEYWORD_TYPE WHERE KEYWORD_ID=?";
		return queryInt(sql, new Object[]{keywordId});
	}
	
	public OperationStatus addRelationshipForMonitorType(int keywordId, int monitorTypeId) {
		if(!(keywordId > 0 && monitorTypeId > 0)) {
			return OperationStatus.ISEMPTY;
		}
		String sql = 
			"INSERT INTO C_KEYWORD_TYPE(ID,KEYWORD_ID,MONITOR_TYPE_ID) " +
			"VALUES(SEQ_C_KEYWORD_TYPE.NEXTVAL,?,?)";
		return insertOrUpdateOrDelete(sql, new Object[]{keywordId,monitorTypeId});
	}
	
	/**{@inheritDoc} */
	public List<MonitorType> getMonitorNothingWithKeyword(int keywordId) {
		String sql = 
				"SELECT A.ID monitorId,A.TYPE monitorType,A.ISSTART isStart "  +
				"FROM C_MONITOR_TYPE A " +
				"WHERE ID NOT IN (" +
					"SELECT MONITOR_TYPE_ID " +
					"FROM C_KEYWORD_TYPE " +
					"WHERE KEYWORD_ID = ?)";
		return queryList(sql, new Object[]{keywordId}, new MonitorTypeParser());
		
	}

	public Keyword get(int id) {
		String sql = "SELECT A.ID keywordId,A.TEXT keywordText "
					+ "FROM C_KEYWORD A "
					+ "WHERE A.ID=?";
		return queryObject(sql, new Object[]{id}, new KeywordParser());
	}

	public List<Keyword> getKeywordByFavoriteId(int favoriteId) {
		String sql = 
				"select b.ID keywordId,b.TEXT keywordText " +
				"from c_website_keyword a,c_keyword b " +
				"where a.favorite_id = ? and a.keyword_id=b.id";
		return queryList(sql, new Object[]{favoriteId}, new KeywordParser());
	}

	public OperationStatus delAllRelationship(int id) {
		String sql = "DELETE C_KEYWORD_TYPE T WHERE T.KEYWORD_ID = ?";
		return insertOrUpdateOrDelete(sql, new Object[]{id});
	}

	public List<Keyword> getKeyword(String text) {
		String sql = 
			" SELECT a.ID keywordId,A.TEXT keywordText" +
			" FROM C_KEYWORD A " +
			" WHERE A.text like '"+text+"%' and ROWNUM <= 10 ";
		return queryList(sql,null, new KeywordParser());
	}
}
